use axum::extract::Json;
use validator::Validate;

use crate::domain::form::user::CreateUserForm;
use crate::domain::request::user::RegisterMemberUserByPhoneAndPasswordRequest;
use crate::domain::response::user::UserMasterResponse;
use crate::prelude::{ApiResponse, BizError};
use crate::service::UserService;

#[utoipa::path(
    post,
    path = "/user/register/phone_password",
    tag = "Core/会员用户",
    summary = "会员用户注册（手机号+密码）",
    description = "通过手机号和密码注册一个会员用户",
    operation_id = "core_user_register_by_phone_and_password",
    request_body = RegisterMemberUserByPhoneAndPasswordRequest,
    params(
        ("phone" = String, Path, description = "phone number"),
        ("password" = String, Path, description = "password"),
    ),
    responses(
        (status = 200, description = "get user detail info successfully", body = [ApiResponse<UserMasterResponse>]),
        (status = 404, description = "not found user by id", body = BizError)
    )
)]
pub async fn member_register_by_phone_and_password(
    Json(req): Json<RegisterMemberUserByPhoneAndPasswordRequest>,
) -> ApiResponse<UserMasterResponse> {
    if let Err(e) = req.validate() {
        return ApiResponse::response_biz_error(e.into());
    }
    let form: CreateUserForm = req.into();
    let result = UserService::member_register_by_phone_and_password(&form)
        .await
        .map(|model| model.into());
    ApiResponse::from(result)
}
